<template>
  <div>
    <!--表格栏-->
    <el-table
      :data="content"
      :highlight-current-row="highlightCurrentRow"
      @selection-change="selectionChange"
      @current-change="handleCurrentChange"
      v-loading="loading"
      :element-loading-text="$t('action.loading')"
      :show-overflow-tooltip="showOverflowTooltip"
      :max-height="maxHeight"
      :size="size"
      :align="align"
    >
      <el-table-column type="selection" width="50" v-if="showSelection" fixed="left"></el-table-column>
      <!-- <el-table-column type="index" width="50" v-if="showIndex"></el-table-column> -->

      <template v-for="column in columns">
        <el-table-column
          header-align="center"
          align="center"
          :prop="column.prop"
          :label="column.label"
          :width="column.width"
          :min-width="column.minWidth"
          :fixed="column.fixed"
          :key="column.prop"
          :type="column.type"
          :formatter="column.formatter"
        >
          <template slot-scope="scope">
            <template v-if="!column.render">
              <template v-if="column.formatter">
                <div
                  v-html="column.formatter(scope.row, column,scope.row[column.prop],scope.index)"
                ></div>
              </template>
              <template v-else>
                <span>{{scope.row[column.prop]}}</span>
              </template>
            </template>
            <template v-else>
              <expand-dom :column="column" :row="scope.row" :render="column.render" :index="index"></expand-dom>
            </template>
          </template>
        </el-table-column>
      </template>

      <el-table-column
        :label="$t('action.operation')"
        :width="operationWidth"
        fixed="right"
        v-if="showOperation"
        header-align="center"
        align="left"
      >
        <template slot-scope="scope">
          <!-- <kt-button
            label="查看"
            :show="[1,2,3,,4,5,6,7,8].includes(scope.row.status) && scope.row.editFlag"
            :size="size"
            type="text"
            @click="handleDetail(scope.$index, scope.row)"
          />
          <kt-button
            label="查看"
            :show="!scope.row.editFlag"
            :size="size"
            type="text"
            @click="handleDetail(scope.$index, scope.row)"
          />
          <kt-button
            label="复制"
            :show="[1,2,3,,4,5,6,7,8].includes(scope.row.status)  && scope.row.editFlag"
            :size="size"
            type="text"
            @click="handleCopyData(scope.$index, scope.row)"
          /> -->

          <kt-button
            v-for="(btn,index) in btnList"
            :key="index"
            :label="btn.customLabel"
            :show="btn.isShowMethod.indexOf(scope.row.statusType==1?scope.row.retailStatus:(scope.row.statusType==2?scope.row.workStatus:scope.row.managerStatus)) >= 0 && scope.row.editFlag"
            :perms="btn.perms"
            :size="size"
            type="text"
            class="optionBtnClass"
            @click="clickFun(btn.method,scope.$index, scope.row)"
          />

          <kt-button
            label="编辑"
            :show="scope.row.businessStatus==1 && scope.row.statusType==3 "
            :size="size"
            type="text"
            @click="businessStatusEdit(scope.$index, scope.row)"
          />

          <kt-button
            label="清退"
            :show="scope.row.businessStatus==2 && scope.row.statusType==3"
            :size="size"
            type="text"
            @click="handleLinkData(scope.$index, scope.row)"
          />
          <kt-button
            label="清退完成"
            :show="scope.row.businessStatus==3 && scope.row.statusType==3"
            :size="size"
            type="text"
            @click="handleLinkData(scope.$index, scope.row)"
          />
        </template>
      </el-table-column>
    </el-table>
    <!--分页栏-->
    <div class="toolbar t_r">
      <kt-button
        :label="$t('action.batchDelete')"
        :perms="permsDelete"
        :size="size"
        type="danger"
        @click="handleBatchDelete()"
        :disabled="this.selections.length===0"
        style="float:left;"
        v-if="showBatchDelete & showOperation"
      />

      <el-pagination
        layout="total, sizes, prev, pager, next, jumper"
        @current-change="refreshPageRequest"
        :current-page="pageRequest.pageNum"
        :page-size="pageRequest.pageSize"
        :total="totalSize"
        style="float:right;"
        :page-sizes="[10, 20, 30, 40]"
        @size-change="handleSizeChange"
      ></el-pagination>
    </div>
  </div>
</template>

<script>
import KtButton from "@/views/Core/KtButton";
import { exportExcel } from "@/utils/excel";
import { formatDate } from "@/utils/datetime";

export default {
  name: "JicTable",
  components: {
    KtButton
  },
  props: {
    columns: Array, // 表格列配置
    data: Object, // 表格分页数据
    permsDelete: String, // 删除权限标识
    permsDetail: String, // 详情权限标识
    operationWidth: {
      type: Number,
      default: 250
    },
    size: {
      // 尺寸样式
      type: String,
      default: "mini"
    },
    align: {
      // 文本对齐方式
      type: String,
      default: "left"
    },
    maxHeight: {
      // 表格最大高度
      type: Number,
      default: 900
    },
    height: {
      // 表格最大高度
      type: Number,
      default: 500
    },
    showOperation: {
      // 是否显示操作组件
      type: Boolean,
      default: true
    },
    border: {
      // 是否显示边框
      type: Boolean,
      default: false
    },
    stripe: {
      // 是否显示斑马线
      type: Boolean,
      default: true
    },
    highlightCurrentRow: {
      // // 是否高亮当前行
      type: Boolean,
      default: true
    },
    showOverflowTooltip: {
      // 是否单行显示
      type: Boolean,
      default: true
    },
    showCopyDataOperation: {
      // 是否显示操作组件
      type: Boolean,
      default: true
    },
    showBatchDelete: {
      // 是否显示操作组件
      type: Boolean,
      default: true
    },
    showCopyLinkOperation: {
      // 是否显示操作组件
      type: Boolean,
      default: true
    },
    showDeleteOperation: {
      // 是否显示操作组件
      type: Boolean,
      default: true
    },
    showDeleteParams: {
      // 是否显示操作组件
      type: Boolean,
      default: false
    },
    showDetailOperation: {
      // 是否显示操作组件
      type: Boolean,
      default: false
    },
    showIndex: {
      // 是否显示操作组件
      type: Boolean,
      default: false
    },
    showSelection: {
      // 是否显示操作组件
      type: Boolean,
      default: true
    },
    loading: {
      type: Boolean,
      default: false
    },
    btnList: {
      type: Array
    },
    statusype: {
      type: Boolean
    }
  },
  data() {
    return {
      totalSize: 0,
      content: [],
      // 分页信息
      pageRequest: {
        pageNum: 1,
        pageSize: 10
      },
      // loading: false, // 加载标识
      selections: [], // 列表选中列
      mock_title: null
    };
  },
  methods: {
    restPage(){
      this.pageRequest.pageNum = 1;
      this.pageRequest.pageSize = 10;
    },
    // 静态页面请求
    // 分页查询
    findPage: function(res) {
      // this.loading = true
      this.content = res.content;
      this.totalSize = Number(res.total);
      // this.loading = false
      // this.$emit("findPage");
    },
    //根据日期来进行查询
    findPageByDate: function(filters) {
      //
      var this_ = this;
      if (filters == undefined || filters == null) {
        filters = {};
      }
      this.utils.request.queryUserList(filters, function(res) {
        if (res.data == null) {
          res.data = [];
        }
        this_.content = res.data;
        this_.totalSize = res.data.length;
      });
    },

    //excle导出
    exportData(filters, filterColumns, fileName) {
      if (filters == undefined || filters == null) {
        filters = {};
      }
      filters.start = null;
      filters.limit = null;
      this.utils.request.queryUserPage(filters, function(res) {
        if (fileName == undefined || fileName == null) {
          fileName = "excel-list";
        }

        fileName = fileName + "_" + formatDate(new Date(), "yyyyMMdd");

        exportExcel(res.rows, filterColumns, fileName);
      });
    },
    init: function() {
      this.$emit("findPage", {
        start: this.pageRequest.pageNum,
        limit: this.pageRequest.pageSize
      });
    },
    // 选择切换
    selectionChange: function(selections) {
      this.selections = selections;
      this.$emit("selectionChange", { selections: selections });
    },
    // 选择切换
    handleCurrentChange: function(val) {
      this.$emit("handleCurrentChange", { val: val });
    },
    // 换页刷新
    refreshPageRequest: function(pageNum) {
      this.pageRequest.pageNum = pageNum;
      this.init();
    },
    // 换页刷新
    handleSizeChange: function(pageSize) {
      this.pageRequest.pageSize = pageSize;
      this.pageRequest.pageNum = 1;
      this.init();
    },
    // 复制活动
    handleCopyData: function(index, row) {
      this.$emit("handleCopyData", { index: index, row: row });
    },
    //复制抽奖链接
    handleLinkData: function(index, row) {
      this.$emit("handleLinkData", { index: index, row: row });
    },
    businessStatusEdit: function(index, row) {
      this.$emit("businessStatusEdit", { index: index, row: row });
    },
    // 删除
    handleDelete: function(index, row) {
      this.delete(row.id);
    },
    // 删除
    handleDeleteParams: function(index, row) {
      this.deleteParams(row);
    },
    // 批量删除
    handleBatchDelete: function() {
      let ids = this.selections.map(item => item.id).toString();
      this.delete(ids);
    },
    handleDetail: function(index, row) {
      this.$emit("handleDetail", { index: index, row: row });
    },
    clickFun: function(method, index, row) {
      this.$emit(method, { index: index, row: row });
    },
    // 删除操作
    delete: function(ids) {
      this.$confirm("确认删除选中记录吗？", "提示", {
        type: "warning"
      })
        .then(() => {
          let params = [];
          let idArray = (ids + "").split(",");
          for (var i = 0; i < idArray.length; i++) {
            params.push({ id: idArray[i] });
          }
          //this.loading = true
          let callback = res => {
            if (res.code == "0000") {
              this.$message({ message: "删除成功", type: "success" });
              this.init();
            } else {
              this.$message({ message: "操作失败, " + res.msg, type: "error" });
            }
            this.loading = false;
          };
          this.$emit("handleDelete", { params: params, callback: callback });
        })
        .catch(() => {});
    },
    // 删除操作 多参数
    deleteParams: function(row) {
      this.$confirm("确认删除选中记录吗？", "提示", {
        type: "warning"
      })
        .then(() => {
          //this.loading = true
          let callback = res => {
            if (res.code == "0000") {
              this.$message({ message: "删除成功", type: "success" });
              this.init();
            } else {
              this.$message({ message: "操作失败, " + res.msg, type: "error" });
            }
            this.loading = false;
          };
          this.$emit("handleDeleteParams", { params: row, callback: callback });
        })
        .catch(() => {});
    }
  },

  mounted() {
    this.refreshPageRequest(1);
  }
};
</script>

<
<style lang="scss" scoped>
  /deep/ .el-table .sort-caret.ascending {
    border-bottom-color: #FFF;
    top: 5px;
  }

  /deep/ .el-table .sort-caret.descending {
    border-top-color: #FFF;
    bottom: 7px;
  }

  /deep/ .el-table .ascending .sort-caret.ascending {
    border-bottom-color: #409eff;
  }

  /deep/ .el-table .descending .sort-caret.descending {
    border-top-color: #409eff;
  }
  .optionBtnClass{
	  margin-top: 5px;
	  margin-bottom: 5px;
  }
</style>
